package paylayer

import (
    "fmt"
	"strconv"
	"strings"
	"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/yabandpay"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)


func OrderYabandPay(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	
	mchId := acPay.MchId
	secreCode := acPay.Appkey
	currency := acPay.MediaId
	user := acPay.SubMchId
	orderStr := strconv.FormatInt(order.OrderId,10)
	des := "YabandPay Pay Order"
	incompleteUrl := beego.AppConfig.String("WXPayNotify") + "/public/yabandpay/notify?merchant_id=%s"
	notifyUrl := fmt.Sprintf(incompleteUrl, mchId)
	payment := strconv.FormatFloat(float64(order.Payment), 'f', -1, 32)
	beego.Debug("Yaband Start MicroAppPay:", orderStr)
	tmpRsp, err := yabandpay.MicroAppPay(secreCode,orderStr,payment,currency,des,user,openId,appId,notifyUrl)
	if err != nil {
		err = errors.Wrap(err, "Yaband Pay MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	beego.Debug("Yaband End MicroAppPay:", orderStr)
	yabandRsp := tmpRsp.RspData.Parameters
	prepayId = strings.Replace(yabandRsp.Package, "prepay_id=", "", -1)
	//更新订单支付方式编号
	order.PayWayCode = 10
	shopping.UpdateOrderPayWayCode(order)
	payRsp := misc.WxResponse{}
	payRsp.AppId = yabandRsp.AppId
	payRsp.NonceStr = yabandRsp.NonceStr
	payRsp.Package = yabandRsp.Package
	payRsp.SignType = yabandRsp.SignType
	payRsp.TimeStamp = yabandRsp.TimeStamp
	payRsp.PaySign = yabandRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderStr
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}


func ChargeYabandPay(appId,openId,outTradeNo string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string,err error){
   
    mchId := acPay.MchId
	secreCode := acPay.Appkey
	currency := acPay.MediaId
	user := acPay.SubMchId
	des := "YabandPay Charge Order"
	incompleteUrl := beego.AppConfig.String("WXPayNotify") + "/public/yabandpay/notify?merchant_id=%s"
	notifyUrl := fmt.Sprintf(incompleteUrl, mchId)
	payment := strconv.FormatFloat(float64(amount), 'f', -1, 32)
	tmpRsp, err := yabandpay.MicroAppPay(secreCode,outTradeNo,payment,currency,des,user,openId,appId,notifyUrl)
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	yabandRsp := tmpRsp.RspData.Parameters
	prepayId = strings.Replace(yabandRsp.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = yabandRsp.AppId
	payRsp.NonceStr = yabandRsp.NonceStr
	payRsp.Package = yabandRsp.Package
	payRsp.SignType = yabandRsp.SignType
	payRsp.TimeStamp = yabandRsp.TimeStamp
	payRsp.PaySign = yabandRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil {
		rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonYabandPay(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string, err error){
    mchId := acPay.MchId
	secreCode := acPay.Appkey
	currency := acPay.MediaId
	user := acPay.SubMchId
	des := "YabandPay Commom Pay Order"
	incompleteUrl := beego.AppConfig.String("WXPayNotify") + "/public/yabandpay/notify?merchant_id=%s"
	notifyUrl := fmt.Sprintf(incompleteUrl, mchId)
	payment := strconv.FormatFloat(float64(amount), 'f', -1, 32)
	tmpRsp, err := yabandpay.MicroAppPay(secreCode,orderId,payment,currency,des,user,openId,appId,notifyUrl)
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	yabandRsp := tmpRsp.RspData.Parameters
	prepayId = strings.Replace(yabandRsp.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = yabandRsp.AppId
	payRsp.NonceStr = yabandRsp.NonceStr
	payRsp.Package = yabandRsp.Package
	payRsp.SignType = yabandRsp.SignType
	payRsp.TimeStamp = yabandRsp.TimeStamp
	payRsp.PaySign = yabandRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
